function [A,Q]=s3db_read(Q)

%S3DB_READ reads S3DB calls into a structure
%Syntax: [A,Q]=s3db_read(Q)
%Description: This is the main function to interact with S3DB's API.
%
%             If Q is a URL, it is passed into URL read. The results of the
%             call are then converted from the default HTML format into a
%             structured variable where each column header was used as a
%             field name.
%
%             If Q is a structure then the different components of the call
%             will be retrieved from specialized fields: 
%             Q.key - access key as provided by S3DB
%             Q.url - URL of target S3DB deployment
%             Q.X - XML query formated to use S3DB native API
%             Q. ... alternatives to the native XML API syntax can be
%                    inserted here and read first to generate Q.X
%                    automatically.
%
%
%Jonas Almeida, Jan 2008
%Romesh Stanislaus April 2010

Q_str='';
if isstruct(Q)
    if isfield(Q,'X')
        Q.X = url_encode(Q.X);
        Q_str=[Q.url,'/S3QL.php?key=',Q.key,'&query=',Q.X];
    else
        %error('this translation into S3DB''s API syntax was not coded yet')
        Q_str=[Q.url,'/S3QL.php?key=',Q.key];
        %Q_str=regexprep(Q_str,'\/\/','\/');
        %Q_str=regexprep(Q_str,':/',':\/\/');
    end
else
    if ischar(Q)
        %most likely entered as a url
        Q_str = Q;
    else
        error('entered format most likely not a string input...!')
        
    end
end

try
    A=urlread(Q_str); %
    A=htmltable2cell(A); %convert it in structure, fields defined as the contents of first row
catch
    error('Entered format not recognizable!')
end
